<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border = "false">
      <template>
        <van-button @click="isEdit = !isEdit" plain round color="#f00" class="edit-btn" type="primary" size="small">
          {{isEdit ? '完成': '编辑'}}
        </van-button>
      </template>
    </van-cell>
    <van-grid class="my-grid" :gutter="10" :border = "false">
      <van-grid-item
        :class="{active: index === active}"
        :text="item.name"
        :icon = "isEdit && index !== 0 ? 'close' : ''"
        v-for="(item, index) in chalass" :key="item.id"
        @click="onMyClick(index,item.id)"
      />
    </van-grid>

    <van-cell title="推荐频道" :border = "false"></van-cell>
    <van-grid class="my-grid" icon-size = "0.3rem" direction="horizontal" :gutter="10" :border = "false">
      <van-grid-item @click="onRecommendChannels(item)" v-for="item in recommendChannels" :key="item.id" icon="plus" :text="item.name" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannel } from '@/api/article.js'
import { addUserChannelItem, deleteSpecifyDelete } from '@/api/user.js'
import { mapState } from 'vuex'
import { setStorage } from '@/utils/storage.js'
import { Toast } from 'vant'
export default {
  name: 'ljw',
  props: {
    chalass: {
      type: Array,
      require: true
    },
    active: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      isEdit: false,
      AllChannel: []
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      const arr = this.AllChannel.filter((item) => {
        const flag = this.chalass.some((item2) => {
          return item2.id === item.id
        })
        return !flag
      })
      return arr
    }
  },
  created () {
    this.onAllChannel()
  },

  methods: {
    onMyClick (index, id) {
      if (this.isEdit) {
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('MyClick')
        }
        this.chalass.splice(index, 1)
        this.deleteChannelStorage(id)
      } else {
        this.$emit('MySelect', index)
      }
    },
    async deleteChannelStorage (id) {
      if (!this.user) return setStorage('MY-CHANNELS', this.chalass)
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      try {
        await deleteSpecifyDelete(id)
        Toast.success('删除成功')
      } catch (err) {
        Toast.fail('删除失败')
      }
    },
    async onAllChannel () {
      try {
        const res = await getAllChannel()
        console.log(res)
        this.AllChannel = res.channels
      } catch (err) {
        console.log('获取失败')
      }
    },
    async onRecommendChannels (obj) {
      this.chalass.push(obj)
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      if (this.user) {
        try {
          await addUserChannelItem({
            id: obj.id,
            seq: this.chalass.length
          })
          Toast.success('添加成功')
        } catch (err) {
          Toast.fail('添加失败')
        }
      } else {
        setStorage('MY-CHANNELS', this.chalass)
        Toast.success('添加成功')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.channel-edit {
  .edit-btn {
    width: 104px;
    height: 48px;
  }
  /deep/.my-grid {
    .van-grid-item {
      width: 160px;
      height: 86px;
      border-radius: 6px;
    }
    .van-grid-item__content {
      background: #F4F5F6;
    }
    .van-icon-close {
      font-size: 35px;
      position: absolute;
      top: -17px;
      right: 5px;
    }
    .van-grid-item__icon+.van-grid-item__text {
      margin-top: 0;
    }
    .active {
      .van-grid-item__text {
        color: #f00;
      }
    }
  }

}
</style>
